from matplotlib import pyplot
import numexpr
import numpy

def draw_cut(h, cut,
             hist_kwargs={}, 
             vspan_kwargs={'color': 'k', 
                           'alpha': '0.3'}):

    if type(cut) != type('hello!'):
        cut = cut.pos_str

    if len(h.assoc_cols) == 1:
        x = numpy.linspace(h.arange[0], h.arange[1], 100000)
        rdict = {h.assoc_cols[0]: x}
    else:
        raise NotImplementedError("Don't know how to draw 2D cuts yet")

    tf = numexpr.evaluate(cut, local_dict=rdict)

    fail_indices = map(int, numpy.argwhere(~tf))

    fail_regions = [[fail_indices[0]]]
    fri = 0
    for i in fail_indices[1:]:
        if i != fail_regions[fri][-1] + 1:
            fail_regions.append([i])
            fri += 1
        else:
            fail_regions[fri].append(i)

    h.draw(hold=True, **hist_kwargs)
    
    for fr in fail_regions:
        low = fr[0]
        high = fr[-1]

        if low == 0:
            low = h.arange[0]
        else:
            low = x[low]

        if high == (len(x) - 1):
            high = h.arange[1]
        else:
            high = x[high]
        
        pyplot.axvspan(low, high, **vspan_kwargs)
    
            
    return h.save_and_clear()
